/**
 * jims
 */
package com.jims.master.modules.csm.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.csm.dao.ExpImportMasterDao;
import com.jims.master.modules.csm.dao.ExpStorageDeptDao;
import com.jims.master.modules.csm.entity.ExpImportMaster;
import com.jims.master.modules.csm.entity.ExpStock;
import com.jims.master.modules.csm.entity.ExpStorageDept;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 入库主记录Service
 *
 * @author zhuq
 * @version 2017-03-16
 */
@Service
@Transactional(readOnly = true)
public class ExpImportMasterService extends CrudService<ExpImportMasterDao, ExpImportMaster> {

    @Autowired
    private ExpStorageDeptDao expStorageDeptDao;
    @Autowired
    private ExpImportMasterDao expImportMasterDao;

    public ExpImportMaster get(String id) {
        return super.get(id);
    }

    public List<ExpImportMaster> findList(ExpImportMaster expImportMaster) {
        return super.findList(expImportMaster);
    }

    public Page<ExpImportMaster> findPage(Page<ExpImportMaster> page, ExpImportMaster expImportMaster) {
        return super.findPage(page, expImportMaster);
    }

    @Transactional(readOnly = false)
    public int save(ExpImportMaster expImportMaster) {
        return super.save(expImportMaster);
    }

    @Transactional(readOnly = false)
    public int delete(ExpImportMaster expImportMaster) {
        return super.delete(expImportMaster);
    }

    //根据传来的expStock数据组装成expImportMaster数据
    public ExpImportMaster createData(ExpStock expStock) {
        ExpImportMaster expImportMaster = new ExpImportMaster();
        expImportMaster.setDocumentNo(expStock.getDocumentNo());            // 入库单号
        expImportMaster.setStorageId(expStock.getStorageId());            // 库存单位
       /* expImportMaster.setStorageFrom(expStock.getStorageFrom());*/
        expImportMaster.setAccountType(expStock.getAccountType());
        expImportMaster.setImportDate(new Date());                            // 入库日期
     /*   expImportMaster.setSupplier(expStock.getBatchNo());		        // 供货方*/
        expImportMaster.setImportClass(DictUtils.getDictLabel(expStock.getImportClass(), "EXP_STOTARG_TYPE", ""));  // 入库类别
        if (expStock.getImportClass().equals("0")) {
            expImportMaster.setSupplier(expStock.getSupplier());
        } else {
            expImportMaster.setStorageFrom(expStock.getSupplier());
        }
       // expImportMaster.setImportDate(expStock.getImportDate());
        expImportMaster.updateOrgId();
        expImportMaster.preInsert();
        return expImportMaster;
    }


    /*
    * 根据传来的expImportMaster数据生成入库单据号
    * @ZhuQ
    * */
    public String createDocumentNo(ExpImportMaster expImportMaster) {
        //根据库房获取入库单前缀
        ExpStorageDept expStorageDept = expStorageDeptDao.get(expImportMaster.getStorageId());
        String documentNo = expStorageDept != null ? expStorageDept.getImportNoPrefix() : "";
        //获取当前日期
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        documentNo = documentNo + sdf.format(d);
        expImportMaster.setDocumentNo(documentNo);
        //序号
        int i = dao.findDocumentNo(expImportMaster);
        String num = String.valueOf(i);
        String str = "0000";
        str = str.substring(0, 4 - num.length()) + num;
        documentNo = documentNo + str;
        return documentNo;
    }

    /**
     * 入库记录查询
     *
     * @param
     * @param expImportMaster
     * @return
     */
    public Page<ExpImportMaster> findImportList(Page<ExpImportMaster> page, ExpImportMaster expImportMaster) {
        expImportMaster.updateOrgId();
        expImportMaster.setPage(page);
        expImportMaster.setStorageId(UserUtils.getUser().getOrgDept().getId());
        page.setList(expImportMasterDao.findImportList(expImportMaster));
        return page;
    }

    /**
     * 消耗品出库数据统计
     * @param importDate,orgId
     * @return
     * @author ZhuQ
     */
    public List<BaseDto> expImportCount(String importDate,String orgId){
        return expImportMasterDao.expImportCount(importDate,orgId);
    }

}